package com.hualan.service.impl;

import com.hualan.pojo.PubUser;
import com.hualan.mapper.PubUserMapper;
import com.hualan.service.PubUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Mapper
public class PubUserServiceImpl implements PubUserService {
    // 只保留字段注入
    @Autowired
    private PubUserMapper pubUserMapper;

    @Override
    public PageInfo<PubUser> getUserList(PubUser pubUser, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PubUser> userList = pubUserMapper.selectUserList(pubUser);
        return new PageInfo<>(userList);
    }

    @Override
    public int addUser(PubUser pubUser) {
        return pubUserMapper.insertUser(pubUser);
    }

    @Override
    public int deleteUserById(Integer id) {
        return pubUserMapper.deleteUserById(id);
    }

    @Override
    public int deleteUsersByIds(List<Integer> ids) {
        return pubUserMapper.deleteUsersByIds(ids);
    }
    @Override
    public int editUser(PubUser pubUser) {
        return pubUserMapper.updateUser(pubUser);
    }

    //-----------------------------------------------

    private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");
    private static final Pattern PASSWORD_PATTERN = Pattern.compile("^(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z\\d]{6,16}$");

    @Override
    public PubUser login(String username, String password) {
        PubUser user = pubUserMapper.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    @Override
    public boolean register(PubUser user) {
        if (!isValidPhone(user.getPhone()) || !isValidPassword(user.getPassword())) {
            return false;
        }
        return pubUserMapper.save(user) > 0;
    }

    @Override
    public boolean bindPhone(int id, String phone) {
        if (!isValidPhone(phone)) {
            return false;
        }
        return pubUserMapper.updatePhone(id, phone) > 0;
    }

    @Override
    public boolean changePassword(int id, String oldPassword, String newPassword) {
        PubUser user = pubUserMapper.findById(id);
        if (user == null ||!user.getPassword().equals(oldPassword)) {
            return false;
        }
        if (!isValidPassword(newPassword)) {
            return false;
        }
        return pubUserMapper.updatePassword(id, newPassword) > 0;
    }

    @Override
    public PubUser getUserById(int id) {
        return pubUserMapper.findById(id);
    }



    private boolean isValidPhone(String phone) {
        return PHONE_PATTERN.matcher(phone).matches();
    }

    private boolean isValidPassword(String password) {
        return PASSWORD_PATTERN.matcher(password).matches();
    }
    //--------------------------------------
}